<template>
	<view class="uni-container">
		<uni-forms ref="form" labelWidth="100px" v-model="formData" validateTrigger="bind">
			<uni-forms-item name="title" label="标题" required>
				<uni-easyinput v-model="formData.title" :clearable="false" />
			</uni-forms-item>
			<uni-forms-item name="source" label="副标题" required>
				<uni-easyinput v-model="formData.source" :clearable="false" />
			</uni-forms-item>
			<uni-forms-item name="link" label="外链" required>
				<uni-easyinput v-model="formData.link" :clearable="false" placeholder="请输入外链地址" />
			</uni-forms-item>
			<uni-forms-item name="content" label="内容">
				<webEdit v-model="formData.content" :modelValue="formData.content" placeholder="请输入内容" ids="content">
				</webEdit>
			</uni-forms-item>
			<uni-forms-item name="mobile" label="封面图">
				<div>
					<image :src="formData.photo_url" style="width: 160rpx;height: 160rpx;border-radius: 10rpx;"></image>
				</div>
			</uni-forms-item>
			<uni-forms-item name="content" label="阅读量">
				<uni-easyinput v-model="formData.read" :clearable="false" />
			</uni-forms-item>
			<uni-forms-item name="content" label="发布日期">
				<uni-easyinput v-model="formData.create_time" :clearable="false" />
			</uni-forms-item>
			<view class="uni-button-group">
				<button style="width: 100px;background-color: #2979ff;color: #fff;" class="uni-button"
					@click="submit">修改</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;"><button style="width: 100px;"
						class="uni-button">返回</button></navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	const db = uniCloud.database();
	export default {
		data() {
			return {
				showPassword: false,
				formData: {
					"title": "",
					"source": "",
					"content": "",
					"photo_url": "",
					"userName": "",
					"read": "",
					"status": false //默认禁用
				},

			}
		},
		onLoad(e) {
			console.log(e)
			const id = e.id
			this.formDataId = id
			this.getDetail(id)
		},
		methods: {
			/**
			 * 切换重置密码框显示或隐藏
			 */
			trigger() {
				this.showPassword = !this.showPassword
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				uniCloud.callFunction({
					name: 'articleadmin',
					data: {
						type: 'detail',
						id: this.formDataId
					},
					success: res => {
						this.formData = res.result.data[0]
						uni.hideLoading()
					}
				})

			},
			submit() {
				uniCloud.callFunction({
					name: 'update',
					data: {
						dbName: 'article',
						datas: this.formData
					},
					success: res => {
						console.log('res', res)
						if (res.result.status == 0) {
							uni.showToast({
								title: '修改成功！',
							})
							uni.navigateBack({
								delta: 1
							})
						}
					}
				})
			},
			// status 对应文字显示
			parseUserStatus(status) {
				if (status === 0) {
					return '启用'
				} else if (status === 1) {
					return '禁用'
				} else if (status === 2) {
					return '审核中'
				} else if (status === 3) {
					return '审核拒绝'
				} else {
					return '启用'
				}
			}
		}
	}
</script>

<style>
	.reset-password-btn {
		/* height: 100%; */
		line-height: 36px;
		color: #007AFF;
		text-decoration: underline;
		cursor: pointer;
	}

	.cancel-reset-password-btn {
		color: #007AFF;
		padding-right: 10px;
		cursor: pointer;
	}
</style>